package com.newbingo.dto;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.newbingo.dao.imple.IMemberDao;
import com.newbingo.dto.imple.IMemberDto;
import com.newbingo.entity.Member;
import com.newbingo.service.imple.IMemberService;
import com.newbingo.util.Pager;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.*;

public class MemberDto extends UserDto implements IMemberDto, Serializable {
	private String maxBet;
	private String minBet;
	private Integer amount;
	private String accountType;
	private String accountNo;
	private String agentName;
	private boolean isOnline; // 是否在线
	private Integer loginAmount;// 登录次数
	private Integer prizeAmount;// 输赢总额

	private IMemberDao memberDao = null;
	private IMemberService memberService = null;

	/*
	 * 分页查询，返回dto数据的Pager对象
	 */
	public Pager findByPage(int start, int limit, String uname, Integer agentId) {
		Pager pager = new Pager();
		Pager pager_temp = memberDao.findByPager(start, limit, uname, agentId);
		pager.setTotalSize(pager_temp.getTotalSize());
		if (pager_temp.getData() != null && pager_temp.getData().size() > 0) {
			List<Member> members = pager_temp.getData();
			for (Member temp : members) {
				MemberDto memberDto = changeToDto(temp);
				pager.getData().add(memberDto);
			}
		}
		return pager;
	}

	/*
	 * 根据会员ID查询出会员，返回DTO对象
	 */
	public MemberDto findById(Integer uno) {
		Member member = memberDao.findById(uno);
		return changeToDto(member);
	}

	/**
	 * 会员登录
	 * 
	 * @param uname
	 *            登录名
	 * @param upwd
	 *            密码
	 * @return
	 */
	public MemberDto findByLogin(String uname, String upwd) {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getRemoteAddr();
		Member member = memberDao.findByLogin(uname, upwd);
		Date date = new Date();
		if (member != null) {
			member.setLastLoginIp(ip);
			member.setLastLogTime(date);
			if (member.getLoginAmount() == null) {
				member.setLoginAmount(1);
			} else {
				member.setLoginAmount(member.getLoginAmount() + 1);
			}
			memberService.updateMember(member);
			return changeToDto(member);
		} else {
			return null;
		}
	}

	// 对象转换方法
	private MemberDto changeToDto(Member temp) {
		MemberDto memberDto = new MemberDto();
		memberDto.setUno(temp.getUno());
		memberDto.setUname(temp.getUname());
		memberDto.setTrueName(temp.getTrueName());
		if (temp.getContactInfo() != null) {
			memberDto.setTel(temp.getContactInfo().getTel());
			memberDto.setEmail(temp.getContactInfo().getEmail());
		}
		memberDto.setStatus(temp.getStatus());
		if (temp.getMaxBet() != null)
			memberDto.setMaxBet(temp.getMaxBet().toString());
		if (temp.getMinBet() != null)
			memberDto.setMinBet(temp.getMinBet().toString());
		if (temp.getAccount() != null) {
			memberDto.setAccountNo(temp.getAccount().getAccountNo());
			memberDto.setAccountType(temp.getAccount().getAccountType());
			memberDto.setAmount(temp.getAccount().getAmount());
		}
		if (temp.getAgent() != null)
			memberDto.setAgentName(temp.getAgent().getTrueName());
		memberDto.setBaseAmount(temp.getBaseAmount());
		memberDto.setBalance(temp.getBalance());
		if (temp.getRegTime() != null) {
			memberDto.setRegTime(temp.getRegTime().toString());
		}
		if (temp.getLastLogTime() != null) {
			memberDto.setLastLoginTime(temp.getLastLogTime().toString());
		}
		memberDto.setLastLoginIp(temp.getLastLoginIp());
		memberDto.setOnline(temp.isOnline());
		memberDto.setLoginAmount(temp.getLoginAmount());
		memberDto.setPrizeAmount(temp.getPrizeAmount());
		return memberDto;
	}

	public String getMaxBet() {
		return maxBet;
	}

	public void setMaxBet(String maxBet) {
		this.maxBet = maxBet;
	}

	public String getMinBet() {
		return minBet;
	}

	public void setMinBet(String minBet) {
		this.minBet = minBet;
	}

	public IMemberDao getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(IMemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public MemberDto() {

	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public String getAccountNo() {
		return accountNo;
	}

	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

	public String getAgentName() {
		return agentName;
	}

	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}

	public boolean isOnline() {
		return isOnline;
	}

	public void setOnline(boolean isOnline) {
		this.isOnline = isOnline;
	}

	public Integer getLoginAmount() {
		return loginAmount;
	}

	public void setLoginAmount(Integer loginAmount) {
		this.loginAmount = loginAmount;
	}

	public Integer getPrizeAmount() {
		return prizeAmount;
	}

	public void setPrizeAmount(Integer prizeAmount) {
		this.prizeAmount = prizeAmount;
	}

	public IMemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(IMemberService memberService) {
		this.memberService = memberService;
	}

}
